package mn.idax.exchange.otc.service.impl;

import com.google.common.collect.Lists;
import mn.idax.exchange.otc.entity.OTCBeneficiarys;
import mn.idax.exchange.otc.entity.OTCBeneficiarysExample;
import mn.idax.exchange.otc.enums.response.OTCBeneficiarysResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.mapper.OTCBeneficiarysMapper;
import mn.idax.exchange.otc.service.FileUrlService;
import mn.idax.exchange.otc.service.OTCBeneficiarysService;
import mn.idax.exchange.otc.util.OTCPayTypeNationUtils;
import mn.idax.exchange.otc.vo.BeneficiarysInfo;
import mn.idax.exchange.otc.vo.OTCBeneficiarysUpdateDisplay;
import mn.idax.exchange.otc.vo.PayMethodDetailDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Auther: shihaiming
 * @Date: 2018/08/06
 * @Description:
 */
@Service
public class OTCBeneficiarysServiceImpl implements OTCBeneficiarysService {

    @Autowired
    private OTCBeneficiarysMapper otcBeneficiarysMapper;

    @Autowired
    private FileUrlService fileUrlService;

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int addOTCBeneficiarys(OTCBeneficiarys otcBeneficiarys) {
    	return otcBeneficiarysMapper.insertSelective(otcBeneficiarys);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updateOTCBeneficiarys(OTCBeneficiarys otcBeneficiarys) {
    	return otcBeneficiarysMapper.updateByPrimaryKeySelective(otcBeneficiarys);
    }


    @Override
    public OTCBeneficiarys getOTCBeneficiarysByuserIdAndDictItemId(Integer userId, Integer dictItemId) {
        OTCBeneficiarysExample example = new OTCBeneficiarysExample();
        example.createCriteria().andUseridEqualTo(userId).andDictitemidEqualTo(dictItemId);
        List<OTCBeneficiarys> otcBeneficiarys = otcBeneficiarysMapper.selectByExample(example);
        return otcBeneficiarys.isEmpty() ? null : otcBeneficiarys.get(0);
    }

    @Override
    public List<PayMethodDetailDTO> getPayMethodDetail(Integer adid) {

        List<PayMethodDetailDTO> payMethodDetailDTO = otcBeneficiarysMapper.getPayMethodDetail(adid);
        return payMethodDetailDTO;
    }


    @Override
	public List<OTCBeneficiarys> selectByUserID(Integer userId) {
		return otcBeneficiarysMapper.selectByUserID(userId);
	}

	@Override
	public OTCBeneficiarys selectByPrimaryKey(Integer id) {
		return otcBeneficiarysMapper.selectByPrimaryKey(id);
	}

	@Override
	public OTCBeneficiarys selectByUserIDAndType(OTCBeneficiarys record) {
		return otcBeneficiarysMapper.selectByUserIDAndType(record);
	}

    @Override
    public List<OTCBeneficiarys> showDisplayBeneficiarys(Integer userId) {
        return otcBeneficiarysMapper.showDisplayBeneficiarys(userId);
    }

    //修改收款方式隐藏/展示
    @Override
    public Integer updateDisplay(OTCBeneficiarysUpdateDisplay updateDisplay) {

        if (!updateDisplay.getIsdisplay()){

            //至少保留一种收款方式
            if (getCountDisplayBeneficiarys(updateDisplay.getUserId()).equals(1)){
                throw new IdaxException(OTCBeneficiarysResponseEnum.OTCBENEFICIARYS_ATLEAST_HAVEONE.getCode());
            }
        }
        OTCBeneficiarysExample example = new OTCBeneficiarysExample();
        example.createCriteria().andUseridEqualTo(updateDisplay.getUserId()).andDictitemidEqualTo(updateDisplay.getDictitemid());
        List<OTCBeneficiarys> otcBeneficiarys = otcBeneficiarysMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(otcBeneficiarys)){
            throw new IdaxException(OTCBeneficiarysResponseEnum.DATA_NOT_EXIST.getCode());
        }

        OTCBeneficiarys updateOTCBeneficiarys = otcBeneficiarys.get(0);
        updateOTCBeneficiarys.setIsdisplay(updateDisplay.getIsdisplay());
        return otcBeneficiarysMapper.updateByPrimaryKeySelective(updateOTCBeneficiarys);
    }

    /**
     * 获取支付方式包含图标和国际化
     * @param userId
     * @return
     */
    @Override
    public List<BeneficiarysInfo> selectPayTypeInfoByUserID(Integer userId) {
        List<OTCBeneficiarys> otcBeneficiarys = otcBeneficiarysMapper.selectByUserID(userId);
        List<BeneficiarysInfo> beneficiarysInfos = Lists.newArrayList();
        for (OTCBeneficiarys otcBeneficiary : otcBeneficiarys) {
            BeneficiarysInfo beneficiarysInfo = new BeneficiarysInfo();
            BeanUtils.copyProperties(otcBeneficiary,beneficiarysInfo);
            beneficiarysInfo.setPayTypeNation(OTCPayTypeNationUtils.getPayType(otcBeneficiary.getDictitemid()));
            beneficiarysInfo.setPayTypeIcon(OTCPayTypeNationUtils.getPayTypeIcon(otcBeneficiary.getDictitemid()));
            beneficiarysInfo.setAttachmenturl(fileUrlService.dealFileUrlAddress(beneficiarysInfo.getAttachmenturl()));
            beneficiarysInfos.add(beneficiarysInfo);
        }
        return beneficiarysInfos;
    }

    @Override
    public Integer getCountDisplayBeneficiarys(Integer userId) {
        return otcBeneficiarysMapper.getCountDisplayBeneficiarys(userId);
    }

    /**
     * 删除收款方式
     * @param id
     * @return
     */
    public boolean deleteOTCBeneficiarys(Integer id){
        int result = otcBeneficiarysMapper.deleteByPrimaryKey(id);
        if(result>0){
            return true;
        }
        return false;
    }

}
